
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const server = http.createServer((req, res) => {
    let urlStr = req.url;
    const data = '';

    let urlObj = url.parse(urlStr, true);
    res.writeHead(200, {
        'content-type': 'application/json; charset=utf-8',
        'Access-Control-Allow-Origin': "*"
    });

    req.on('data', (chunk) => {
        data += chunk;
    })

    req.on('end', () => {
        responseResult(querystring.parse(data))
    })

    function responseResult(querystring) {
        switch (urlObj.pathname) {
            case '/api/data':
                res.write('{"ret":true,"data":"hello"}')
                break;
            default:
                res.end('404....')
                break;
        };
        res.end()
    }

});

server.listen(8080)

